package com.hxkj.utils;

import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.util.HashSet;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

/***
 *  @author yaosy
 *  @desc zip文件打包下载
 *  @date 2018年8月9日
 * **/
public class ZipUtils {

    private static final String DEFAULT_CHARSET = "UTF-8";

    /**
     * 压缩单个文件
     *
     * @param source 源文件
     * @param dest   目的文件
     */
    public static void zipFile(File source, File dest) {
        InputStream inputStream = null;
        ZipOutputStream zipOutputStream = null;
        try {
            inputStream = new FileInputStream(source);
            zipOutputStream = new ZipOutputStream(new FileOutputStream(dest));
            zipOutputStream.putNextEntry(new ZipEntry(source.getName()));
            byte b[] = new byte[1024];
            while (inputStream.read(b) != -1) {
                zipOutputStream.write(b);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                inputStream.close();
                zipOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 压缩多个文件
     *
     * @param dir  源文件目录
     * @param dest 目的文件
     */
    public static void zipFiles(File dir, File dest) {
        if (dir.isDirectory()) {
            zipFiles(dir.listFiles(), dest);
        }
    }

    /**
     * 压缩多个文件
     *
     * @param files 源文件数组
     * @param dest  目的文件
     */
    public static void zipFiles(File[] files, File dest) {
        InputStream inputStream = null;
        ZipOutputStream zipOutputStream = null;
        Set set = new HashSet<>();
        try {
            zipOutputStream = new ZipOutputStream(new FileOutputStream(dest));
            for (File f : files) {
                if( f==null || StringUtils.isEmpty(f.getName()) || set.contains(f.getName())) continue;
                inputStream = new FileInputStream(f);
                zipOutputStream.putNextEntry(new ZipEntry(f.getName()));
                byte b[] = new byte[1024];
                while (inputStream.read(b) != -1) {
                    zipOutputStream.write(b);
                }
                set.add(f.getName());
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                inputStream.close();
                zipOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 解压多个文件
     *
     * @param file 待解压文件
     */
    public static void unzipFiles(File file) {
        String path = file.getParent();
        File outFile = null;
        InputStream inputStream = null;
        OutputStream outputStream = null;
        ZipInputStream zipInputStream = null;
        ZipEntry entry;
        try {
            ZipFile zipFile = new ZipFile(file);
            zipInputStream = new ZipInputStream(new FileInputStream(file));
            while ((entry = zipInputStream.getNextEntry()) != null) {
                outFile = new File(path + File.separator + entry.getName());
                if (!outFile.getParentFile().exists()) {
                    outFile.getParentFile().mkdir();
                }
                if (!outFile.exists()) {
                    outFile.createNewFile();
                }
                inputStream = zipFile.getInputStream(entry);
                outputStream = new FileOutputStream(outFile);
                int temp = 0;
                while ((temp = inputStream.read()) != -1) {
                    outputStream.write(temp);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                inputStream.close();
                outputStream.close();
                zipInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    /***
     * 删除目录文件及子目录文件
     * **/
    public static boolean deletefile(String delpath) throws Exception {
        File file = new File(delpath);
        try {
            // 当且仅当此抽象路径名表示的文件存在且 是一个目录时，返回 true
            if (!file.isDirectory()) {
                file.delete();
            } else if (file.isDirectory()) {
                String[] filelist = file.list();
                for (int i = 0; i < filelist.length; i++) {
                    File delfile = new File(delpath + "\\" + filelist[i]);
                    if (!delfile.isDirectory()) {
                        delfile.delete();
                    } else if (delfile.isDirectory()) {
                        deletefile(delpath + "\\" + filelist[i]);
                    }
                }
                file.delete();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            System.out.println("deletefile() Exception:" + e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
        }
        return true;
    }
}
